#include "BoardMesh.h"

using namespace Game;

BoardMesh::BoardMesh(MeshDesc* data) : DirectX::Mesh(data)
{ 
  m_transparent = 1.0f; 
  SetTranslation(0,0,0);
  SetRotation(0,0,0); 
} 

void BoardMesh::Refresh()
{
  float step = 0.42f;
  float left = -1.48f;
  float bottom = -1.46f;

  AABBDesc* aabb = GetAABB();

  for(unsigned i = 0; i < LEN; ++i)
  {
    for(unsigned j = 0; j < LEN; ++j)
    {
      FieldMesh* field = dynamic_cast<FieldMesh*>(GetField(i, j));
      if(field != 0)
      {
        float x,y,z;
        
        AABBDesc* fieldAABB = field->GetAABB();
        x = left + j * step + m_translation._41;
        y = aabb->Center.y + aabb->Extents.y + 2 * fieldAABB->Extents.y + fieldAABB->Center.y + m_translation._42;
        z = bottom + i * step + m_translation._43;

        field->SetTranslation(x,y,z);
        if(field->Occupied())
        {
          DirectX::Mesh* m = dynamic_cast<DirectX::Mesh*>(field->GetFigure());
          XMMATRIX M = m->GetWorldMatrix();
          m->SetTranslation(x, M._42, z);
        }
      }
    }
  }
}